package com.hqyj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.dao.GoodsDao;
import com.hqyj.pojo.Goods;
import com.hqyj.pojo.PageData;
import com.hqyj.service.GoodsService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class GoodsService2Impl implements GoodsService2  {

    @Autowired
   private  GoodsDao goodsDao;
    @Override
    public PageData showGoodsBypage(Integer pageNumber, Integer pageSize,String name) {

        //使用分页插件 拦截下一次mybatis的查询
        PageHelper.startPage(pageNumber,pageSize);

        //查询用户信息
        String selectname="%"+name+"%";
        List<Goods> allGoods = goodsDao.GetGoodsBylike(selectname);

        //创建pageInfo，会计算分页相关的信息
        PageInfo pageInfo=new PageInfo<>(allGoods);


        //创建我们的数据模型
        PageData pageData=new PageData();



        pageData.setCurrentPage(pageNumber);
        pageData.setPageSize(pageSize);
        //填充总页数数据
        pageData.setTotalPage(pageInfo.getPages());
        //填充总记录数
        pageData.setTotalSize((int)pageInfo.getTotal());

        if(pageInfo.isHasNextPage()){
            //有下一页获取下一页的页码
            pageData.setNextPage(pageInfo.getNextPage());

        }else {
            //没有，就是总页数=尾页
            pageData.setNextPage(pageInfo.getPages());

        }
        if(pageInfo.isHasPreviousPage()){
            //如果有上一页
            pageData.setPreviousPage(pageInfo.getPrePage());

        }else {
            //没有上一页，设置为首页 1
            pageData.setPreviousPage(1);

        }
        //设置每页的记录信息
        pageData.setList(pageInfo.getList());


        return pageData;
    }

    @Override
    public int ModifyGoodsById(Long goodsId, String goodsPic, String goodsName, String goodsDesc, Integer goodsStatus, Long typeId, String attributeList, double goodPrice, Long goodsStock, double discount, String createTime, String updateTime) {
        Goods goods=new Goods();
        goods.setGoodsId(goodsId);
        goods.setGoodsPic(goodsPic);
        goods.setGoodsName(goodsName);
        goods.setGoodsDesc(goodsDesc);
        goods.setGoodsStatus(goodsStatus);
        goods.setTypeId(typeId);
        goods.setAttributeList(attributeList);
        goods.setGoodPrice(goodPrice);
        goods.setGoodsStock(goodsStock);
        goods.setDiscount(discount);
        goods.setCreateTime(createTime);
        goods.setUpdateTime(updateTime);
        return  goodsDao.ModifyGoodsById(goods);

    }

    @Override
    public List<Goods> showGoods() {
        List<Goods> allGoods = goodsDao.getAllGoods();
        return allGoods;

    }

    @Override
    public int updatePicture(String picName, Long id) {
        int i= goodsDao.savePicture(picName,id);
        return i;
    }

    @Override
    public   List<Map<String,String>> selectType() {

        List<Map<String,String>> selecttype = goodsDao.selecttype();

//        for (Map<String,Object> map: selecttype) {
//            Set<Map.Entry<String, Object>> entries = map.entrySet();
//            for (Map.Entry<String, Object> entry: entries) {
//                System.out.println(entry.getKey()+"--"+entry.getValue());
//            }
//
//        }




        return selecttype;
    }

    @Override
    public List<Goods> getAllGoods() {

        List<Goods> allGoods = goodsDao.getAllGoods();
        return allGoods;
    }


}
